//
//  MasterViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MasterViewController.h"

#import "ImageViewController.h"
#import "VideoViewController.h"
#import "NoteViewController.h"
#import "AboutViewController.h"
#import "PreferenceManager.h"

@implementation MasterViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
    return self;
}
							
- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"Login";
    
    
    
    NSString *savedPass = [PreferenceManager getPass];
    if(savedPass == nil){
        pass0.hidden = NO;
        pass0.placeholder = @"Enter new passcode";
        pass.placeholder = @"Re-enter new passcode";
    }
    else{
        
    }
    
    pass0.text = @"";
    pass.text = @"";
}

-(IBAction)btnTest_Click:(id)sender{
    NSString *savedPass = [PreferenceManager getPass];
    NSString *thePass = pass.text;
    NSString *thePass0 = pass0.text;
    
    pass0.text = @"";
    pass.text = @"";
    
    if(savedPass == nil)
    {
        if(thePass != nil && thePass0 != nil && [thePass length] > 0 && [thePass0 length] > 0 && [thePass0 isEqualToString:thePass]){
            [PreferenceManager setPass:thePass];
            [self enterVault];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" 
                                                            message:@"Passcode cannot be empty and you must enter same passcode in both text fields to confirm." 
                                                           delegate:nil cancelButtonTitle:@"Ok" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
    else{
        NSString *login_resp = [PreferenceManager login:thePass];
        if(login_resp == nil){
            [self enterVault];
        }
        else
        {
            NSString* alertTitle;
            
            if([login_resp rangeOfString:@"seconds time"].location == NSNotFound)
            {
                alertTitle = @"Invalid Login!";
            }
            else
            {
                alertTitle = @"Wait!"; 
            }
            
            //if([login_resp contains:[NSString stringWithFormat:EARLY, retry_time_remaining]])
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle                                                                                    message:login_resp 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"Close" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
}

-(void)enterVault{
    ImageViewController *image_vc = [[ImageViewController alloc] init];
    UITabBarItem *image_item = [[UITabBarItem alloc] initWithTitle:@"Photos" image:[UIImage imageNamed:@"121-landscape.png"] tag:1000];
    image_vc.tabBarItem = image_item;
    [image_item release];
    
    
    VideoViewController *video_vc = [[VideoViewController alloc] init];
    UITabBarItem *video_item = [[UITabBarItem alloc] initWithTitle:@"Videos" image:[UIImage imageNamed:@"45-movie-1.png"] tag:1001];
    video_vc.tabBarItem = video_item;
    [video_item release];
    
    NoteViewController *note_vc = [[NoteViewController alloc] init];
    UITabBarItem *note_item = [[UITabBarItem alloc] initWithTitle:@"Notes" image:[UIImage imageNamed:@"96-book.png"] tag:1002];
    note_vc.tabBarItem = note_item;
    [note_item release];
    
    AboutViewController *about_vc = [[AboutViewController alloc] init];
    UITabBarItem *about_item = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"19-gear.png"] tag:1003];
    about_vc.tabBarItem = about_item;
    [about_item release];
    
    
    NSArray *array = [[NSArray alloc] initWithObjects:image_vc, video_vc, note_vc, about_vc, nil];
    [image_vc release];
    [video_vc release];
    [note_vc release];
    [about_vc release];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = array;
    [array release];
    
    self.navigationItem.title = @"Logout";
    [self.navigationController pushViewController:tab animated:NO];
    [tab release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
